package com.qianya.weishi_server.config.security;

import io.jsonwebtoken.Claims;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Collection;


/**
 * @author LLL
 */
public class StatelessTokenAuthentication implements Authentication {

    private static final long serialVersionUID = -9197714992393816021L;
    private Claims claims;
    private Credentials credentials;
    private Boolean authentication = false;

    StatelessTokenAuthentication(HttpServletRequest request, HttpServletResponse response) {
        this.credentials = new Credentials(request, response);
    }

    StatelessTokenAuthentication(Claims claims) {
        this.claims = claims;
        this.authentication = true;
    }

    @Override
    public Collection<GrantedAuthority> getAuthorities() {
        return new ArrayList<>();
    }

    @Override
    public Object getCredentials() {
        return credentials;
    }

    @Override
    public Object getDetails() {
        return null;
    }

    @Override
    public String getName() {
        return claims.getSubject();
    }

    @Override
    public Object getPrincipal() {
        return claims;
    }

    @Override
    public boolean isAuthenticated() {
        return authentication;
    }

    @Override
    public void setAuthenticated(boolean authenticated) {
        this.authentication = authenticated;
    }

    public class Credentials {
        private HttpServletRequest request;
        private HttpServletResponse response;

        Credentials(HttpServletRequest request, HttpServletResponse response) {
            this.request = request;
            this.response = response;
        }

        public HttpServletRequest getRequest() {
            return request;
        }

        public void setRequest(HttpServletRequest request) {
            this.request = request;
        }

        public HttpServletResponse getResponse() {
            return response;
        }

        public void setResponse(HttpServletResponse response) {
            this.response = response;
        }
    }
}
